﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core.Primitives
{
    [Serializable]
    public struct SingleRect
    {
        public SinglePoint LeftBottomPoint;
        public SinglePoint RightTopPoint;

        public float Left
        {
            get
            {
                return LeftBottomPoint.X;
            }
            set
            {
                LeftBottomPoint.X = value;
            }
        }
        public float Right
        {
            get
            {
                return RightTopPoint.X;
            }
            set
            {
                RightTopPoint.X = value;
            }
        }
        public float Bottom
        {
            get
            {
                return LeftBottomPoint.Y;
            }
            set
            {
                LeftBottomPoint.Y = value;
            }
        }
        public float Top
        {
            get
            {
                return RightTopPoint.Y;
            }
            set
            {
                RightTopPoint.Y = value;
            }
        }
        public SinglePoint Center
        {
            get
            {
                return new SinglePoint((Left + Right) / 2.0f, (Bottom + Top) / 2.0f);
            }
            set
            {
                SinglePoint shift = value - Center;

                LeftBottomPoint += shift;
                RightTopPoint += shift;
            }
        }

        public float Width
        {
            get
            {
                return RightTopPoint.X - LeftBottomPoint.X;
            }
        }
        public float Height
        {
            get
            {
                return RightTopPoint.Y - LeftBottomPoint.Y;
            }
        }

        public SingleRect(SinglePoint leftBottomPoint, SinglePoint rightTopPoint)
        {
            LeftBottomPoint = leftBottomPoint;
            RightTopPoint = rightTopPoint;
        }
        public SingleRect(SinglePoint leftBottomPoint, float width, float height)
        {
            LeftBottomPoint = leftBottomPoint;
            RightTopPoint = leftBottomPoint + new SinglePoint(width, height);
        }

        public bool Overlaps(SingleRect rect)
        {
            return this.Left < rect.Right 
                && this.Right > rect.Left
                && this.Bottom < rect.Top 
                && this.Top > rect.Bottom;
                
        }        
    }
}
